<?php
/**
 * 微信支付模块微站定义
 *
 * @author xingkai
 * @url 
 */
defined('IN_IA') or exit('Access Denied');

class Hckj_payModuleSite extends WeModuleSite {

	public function doWebJilu() {
		global $_W;
		global $_GPC;
		load()->func('tpl');
		$uniacid = $_W['uniacid'];

		$op = $_GPC['op'] ? $_GPC['op'] : 'display';
		$paytype = array('credit' => '余额', 'wechat' => '微信', 'alipay' => '支付宝', 'delivery' => '货到付款');

		$pindex = max(1, intval($_GPC['page']));
		$psize = 20;

		$data = array( ':uniacid' => $_W['uniacid']);

		$lists = pdo_fetchall('select * from '.tablename('hckj_money_log').' where uniacid = :uniacid order by createtime desc',$data );


		$total  = pdo_fetchcolumn('select count(*) from '.tablename("hckj_money_log")." where uniacid = :uniacid order by createtime desc", $data );
		$pager  = pagination($total, $pindex, $psize);

		include $this->template('jilu');
	}

	public function payResult($params)
	{
		global $_W;
		global $_GPC;
		load()->func('tpl');
		if ($params['result'] == 'success' && $params['from'] == 'notify') {   //notify为异步插入,按notify为准
			$paytype = array('credit' => '1', 'wechat' => '2', 'alipay' => '2', 'delivery' => '3');
			$data = array(
				'createtime' => time(),
				'updatetime'    => time(),
				'status'        => 1,
				'openid'        => $params['user'],
				'money'         => $params['fee'],
				'uniacid'       => $_W['uniacid'],
				'orderid'       => $params['tid'],
			);
			$data['paytype'] = $params['type'];
			if ($params['type'] == 'wechat') {
				$data['transid'] = $params['tag']['transaction_id'];
			}
			pdo_insert('hckj_money_log', $data);
		}
		if (empty($params['result']) || $params['result'] != 'success') {
			//此处会处理一些支付失败的业务代码
		}
		//如果消息是用户直接返回（非通知），则提示一个付款成功
		if($params['from'] == 'return') {
			if ($params['type'] == 'credit2') {
				message('已经成功支付',$this->createMobileUrl('index'), 'success');
			} else {
				message('已经成功支付',$this->createMobileUrl('index'), 'success');
			}
		}
	}

	public function doMobileIndex() {
		global $_W;
		global $_GPC;
		load()->func('tpl');
		$uniacid = $_W['uniacid'];
		$op = $_GPC['op'];

		if($op == 'pay') {
			$params['ordersn'] = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
			$params['tid'] = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
			$params['user'] = $_W['openid'];
			$params['fee'] = $_GPC['money'];
			$params['title'] = '物业总费用';
			$_SESSION['type'] = 'profree';

			$this->pay($params);exit;
		}

		include $this->template('index');
	}

}